package com.tengju.bff.interfaces.manage.crm.activity;

import com.tengju.bff.interfaces.manage.common.facade.CommonManageFacade;
import com.tengju.bff.interfaces.manage.crm.activity.facade.ActivityTargetAnalysisFacade;
import com.tengju.bff.interfaces.manage.crm.activity.form.ActivityTeamQuery;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.render.FunnelVO;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.data.domain.model.clickhouse.crm.UserRole;
import com.tengju.export.application.ExportService;
import com.tengju.export.domain.record.ExportDataType;
import com.tengju.export.domain.record.ExportFileType;
import com.tengju.user.domain.model.user.UserIdCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author chenhang
 * @Date 2021/10/13
 */
@Validated
@RestController
@Api(tags = "生意参谋-销售活动目标-经销商视角")
@RequestMapping(value = "manage/crm/activity/analysis/dealer")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ActivityDealerAnalysisController {

    private final ActivityTargetAnalysisFacade activityTargetAnalysisFacade;
    private final ExportService exportService;
    private final CommonManageFacade commonManageFacade;



    /******************************           （经销商视角）活动查看         *********************************/


    @ApiOperation(value = "（经销商）活动转化漏斗", notes = "销售目标table")
    @RequestMapping(value = "/dealerTransformFunnel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO>> dealerTransformFunnel(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.DEALER);
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.transformFunnel(query));
    }

    @ApiOperation(value = "（经销商）加购商品排行榜", notes = "销售目标table")
    @RequestMapping(value = "/dealerAddcartGoodsRankTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> dealerAddcartGoodsRankTable(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.DEALER);
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.addcartGoodsRankTable(query));
    }

    @ApiOperation(value = "（经销商）出货商品排行榜", notes = "销售目标table")
    @RequestMapping(value = "/dealerBuyGoodsRankTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> dealerBuyGoodsRankTable(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.DEALER);
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.moneyoutRankTable(query));
    }

    @ApiOperation(value = "经销商目标达成", notes = "经销商目标达成")
    @RequestMapping(value = "/dealerTargetReachTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> dealerTargetReachTable(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.DEALER);
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.dealerTargetReachTable(query));
    }

    @ApiOperation(value = "下载经销商目标达成", notes = "下载经销商目标达成")
    @RequestMapping(value = "/exportDealerTargetReachTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<Object> exportDealerTargetReachTable(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.DEALER);
        long staffId = ServletContextUtil.getServletContext().getStaffId().getValue();
        String realName = ServletContextUtil.getServletContext().getRealName();
        exportService.exportData("经销商目标达成数据导出", ExportFileType.XLSX,
                "dealerTargetReachExportDataSourceRepository",
                ExportDataType.TABLE, query,
                staffId, realName);
        return ApiResponse.newSuccess();
    }


}
